﻿using NCS.IConnect.Core.WebApi.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.Controllers;

namespace NCS.IConnect.Core.WebApi.Description
{
    internal class SearchCriteriaUriParameterExporter: IUriParameterExporter
    {
        private PropertyInfo orderByProperty = typeof(SearchCriteriaBase).GetProperty("OrderBy");
        private PropertyInfo pageSizeProperty = typeof(SearchCriteriaBase).GetProperty("PageSize");
        private PropertyInfo pageIndexProperty = typeof(SearchCriteriaBase).GetProperty("PageIndex");

        public IEnumerable<UriParameterDescriptor> GetParameters(ApiActionDescriptor actionDescriptor, HttpParameterDescriptor httpParameterDescriptor, IModelDocumentationProvider documentationProvider)
        {
            if (typeof(SearchCriteriaBase).IsAssignableFrom(httpParameterDescriptor.ParameterType))
            {
                foreach (var property in httpParameterDescriptor.ParameterType.GetTypeInfo().DeclaredProperties)
                {
                    yield return new UriParameterDescriptor(actionDescriptor)
                    {
                        ParameterName = property.Name.ToCamelCase(),
                        ParameterType = property.PropertyType,
                        IsOptional = true,
                        Description = documentationProvider.GetDocumentation(property),
                        Remark = documentationProvider.GetRemark(property),
                        DefaultValue = property.GetDefaultValue()
                    };
                }

                yield return new UriParameterDescriptor(actionDescriptor)
                {
                    ParameterName = "orderBy",
                    ParameterType = typeof(string),
                    IsOptional = true,
                    Description = Resources.OrderByDescription,
                    Remark = "The format is like this: orderby={fieldname direction}. E.g. orderby=age asc|desc"
                };
                yield return new UriParameterDescriptor(actionDescriptor)
                {
                    ParameterName = "pageIndex",
                    ParameterType = typeof(int),
                    IsOptional = true,
                    Description = Resources.PageIndexDescription,
                    Remark = documentationProvider.GetRemark(pageIndexProperty),
                    DefaultValue = 1
                };

                yield return new UriParameterDescriptor(actionDescriptor)
                {
                    ParameterName = "pageSize",
                    ParameterType = typeof(int),
                    IsOptional = true,
                    Description = Resources.PageSizeDescription,
                    Remark = documentationProvider.GetRemark(pageSizeProperty),
                    DefaultValue = 10
                };
            }
        }
    }
}
